#include "StdAfx.h"
#include "xhImgAnaly.h"
#include "xhImgAnaly.h"

xhImgAnaly::xhImgAnaly()
{
	m_dwContext       = 0;
	m_pContext        = NULL;
	m_funMsgCallBack  = NULL;


	m_nThreadType = eThdType_Nothing;
}

xhImgAnaly::~xhImgAnaly()
{
	UnInitialize();
}

int xhImgAnaly::UnInitialize()
{
	DDThread3::DDThdUnInit();

	DD_AUTO_LOCK xLock(&m_Sect);

	xkImageAnaly::UnInitialize();
	xkRefPointMan::UnInitialize();

	return 0;
}


int xhImgAnaly::Initialize()
{
	xkImageAnaly::Initialize();
	m_xDispRef.SetDisplayPalette(0,65535,16);
	m_xDispSap.SetDisplayPalette(0,65535,16);
	m_xDispAdj.SetDisplayPalette(0,65535,16);
	m_xDispSub.SetDisplayPalette(0,20000,16);

	xkRefPointMan::Initialize();

	DDThread3::DDThdInit();

	return DD_ERR_SUCCESS;
}

int xhImgAnaly::SetHwnd(int nWindow,HWND hWnd,zkScreenMon *pScreen)
{
	switch( nWindow )
	{
	case eAnalyWindow_Ref:
		{
			m_xDispRef.SetParent( hWnd,pScreen );
		}
		break;
	case eAnalyWindow_Sap:
		{
			m_xDispSap.SetParent( hWnd,pScreen );
		}
		break;
	case eAnalyWindow_Adj:
		{
			m_xDispAdj.SetParent( hWnd,pScreen );
		}
		break;
	case eAnalyWindow_Sub:
		{
			m_xDispSub.SetParent( hWnd,pScreen );
		}
		break;
	default:
		break;
	}

	return DD_ERR_SUCCESS;
}

int xhImgAnaly::RegOnDrawCallBack(AnalyOnDrawCallBackFun funCallBack,void *pContext)
{
	int nRet = 0;
	nRet |= m_xDispRef.RegOnDrawCallBack(funCallBack,eAnalyWindow_Ref,pContext);
	nRet |= m_xDispSap.RegOnDrawCallBack(funCallBack,eAnalyWindow_Sap,pContext);
	nRet |= m_xDispAdj.RegOnDrawCallBack(funCallBack,eAnalyWindow_Adj,pContext);
	nRet |= m_xDispSub.RegOnDrawCallBack(funCallBack,eAnalyWindow_Sub,pContext);
	return nRet;
}

int xhImgAnaly::RegMsgCallBack(AnalyMsgCallBackFun funCallBack,void *pContext)
{
    DD_AUTO_LOCK xLock(&m_Sect);

	m_dwContext       = 0;
	m_pContext        = pContext;
	m_funMsgCallBack  = funCallBack;

	return 0;
}


int xhImgAnaly::SetTemplateType(int nType)
{
	int nRet = xkRefPointMan::SetTemplateType( nType );
	if( DD_ERR_SUCCESS == nRet )
	{
		if( NULL != m_funMsgCallBack )
		{
			m_funMsgCallBack(AnalyMsg_LoadTemplate,100,0,0,m_pContext);
		}  
		return nRet;
	}

	m_nThreadType = eThdType_LoadTemplate;
	DDThread3::SetRunEvent();

	return 0;
}
inline void xhImgAnaly::Thread_LoadTemplate()
{
	DD_AUTO_LOCK xLock(&m_Sect);

	if( NULL != m_funMsgCallBack )
	{
		m_funMsgCallBack(AnalyMsg_LoadTemplate,0,0,0,m_pContext);
	}

	xkRefPointMan::LoadTemplate();

	if( NULL != m_funMsgCallBack )
	{
		m_funMsgCallBack(AnalyMsg_LoadTemplate,100,0,0,m_pContext);
	}  

// 	m_nThreadType = eThdType_Nothing;
// 	DDThread3::SetRunEvent(false);
}







void xhImgAnaly::SetDefaultDispPalette(int nWindow,zkImgBitInfo *pImgInfo)
{
	int nDark   = 0;
	int nBright = 65535;
	int nBpp    = 16;

	switch( pImgInfo->nImgDefine ) 
	{
	case eImage_Define_Img8:
		{
			
		}
		break;
	case eImage_Define_Img16:
		{
			 nBpp = ( (zkImg16BitInfo *)pImgInfo->pImageHeader )->nBitPerSample;
			 nBright = (1<<nBpp) - 1;
		}
		break;
	case eImage_Define_Img32:
		{
		}
		break;
	default:
		break;
	}

	switch( nWindow )
	{
	case eAnalyWindow_Ref:
		{
			m_xDispRef.SetDisplayPalette(nDark,nBright,nBpp);
		}
		break;
	case eAnalyWindow_Sap:
		{
			m_xDispSap.SetDisplayPalette(nDark,nBright,nBpp);
		}
		break;
	case eAnalyWindow_Adj:
		{
			m_xDispAdj.SetDisplayPalette(nDark,nBright,nBpp);
		}
		break;
	case eAnalyWindow_Sub:
		{
			m_xDispSub.SetDisplayPalette(nDark,nBright,nBpp);
		}
		break;
	default:
		break;
	}
}

int xhImgAnaly::LoadRefDataToDisp()
{
	m_nThreadType = eThdType_RefDisplay;
	DDThread3::SetRunEvent();

	return 0;
}

void xhImgAnaly::Thread_LoadRefDataToDisp()
{
	DD_AUTO_LOCK xLock(&m_Sect);
	
	int nRet = -1;

	if( NULL != xkRefPointMan::m_pActivePoint )
	{
		zkImgBitInfo *pImgInfo = NULL;
		nRet = xkRefPointMan::m_pActivePoint->GetOrgImgBuffer( &pImgInfo );
		if( DD_ERR_SUCCESS == nRet )
		{
			nRet = m_xDispRef.SetDisplayData( pImgInfo,&m_xRefDraw );
		}
	}

// 	m_nThreadType = eThdType_Nothing;
// 	DDThread3::SetRunEvent(false);
}



inline void xhImgAnaly::Thread_RefOperate()
{
	DD_AUTO_LOCK xLock(&m_Sect);

	
}


int xhImgAnaly::DDThdDispatch(HANDLE hExitEvent)
{
	switch( m_nThreadType )
	{
	case eThdType_LoadTemplate:Thread_LoadTemplate();      break;
	case eThdType_RefDisplay:  Thread_LoadRefDataToDisp(); break;
	case eThdType_RefOperate:  Thread_RefOperate();        break;
	case eThdType_ChkOperate:  Thread_ChkOperate();        break;
	case eThdType_DstOperate:  Thread_DstOperate();        break;
	default:
		{	
		}
		break;
	}

	m_nThreadType = eThdType_Nothing;
	DDThread3::SetRunEvent(false);

	return 0;
}





int xhImgAnaly::GetImageSize(int &nWidth,int &nHeight)
{
	DD_AUTO_LOCK xLock(&m_Sect);

	int nRet = -1;

	if( NULL != xkRefPointMan::m_pActivePoint )
	{
		nRet = xkRefPointMan::m_pActivePoint->GetImageSize(nWidth,nHeight);
	}

    return nRet;
}



int	xhImgAnaly::Draw(int nWindow,void *pVoid)
{
	DD_AUTO_LOCK xLock(&m_Sect);

	int nRet = 0;

	switch( nWindow )
	{
	case eAnalyWindow_Ref:
		{
			if( NULL != pVoid )
			{
				memcpy(&m_xRefDraw,pVoid,sizeof(m_xRefDraw));
			}

			nRet = m_xDispRef.SetDisplayDst(pVoid);
		}
		break;
	case eAnalyWindow_Sap:
	case eAnalyWindow_Adj:
	case eAnalyWindow_Sub:
		{
			if( NULL != pVoid )
			{
				memcpy(&m_xSapDraw,pVoid,sizeof(m_xSapDraw));
			}

			nRet |= m_xDispRef.SetDisplayDst(pVoid);
			nRet |= m_xDispSap.SetDisplayDst(pVoid);
			nRet |= m_xDispAdj.SetDisplayDst(pVoid);
			nRet |= m_xDispSub.SetDisplayDst(pVoid);
		}
		break;
	default:
		break;
	}

	return nRet;
}

int xhImgAnaly::ImageCtrl(int nWindow,DWORD dwType,void *pVoid)
{
	DD_AUTO_LOCK xLock(&m_Sect);

	int nRet = 0;

	switch( dwType )
	{
	case ImageCtrl_ClearScreen:
		nRet = m_xDispRef.ClearScreen(0x07);
		break;
	case ImageCtrl_Histogram:
		{
           if( NULL == pVoid ) 
		   {
			   nRet = DD_ERR_PARAMETER;
			   break;
		   }

		   int *pTemp = (int *)pVoid; 

		   int nLeft  = *pTemp;
		   int nRight = *(pTemp+1);
		   int nBpp   = m_xDispRef.m_nBpp;

		   nRet = m_xDispRef.SetDisplayPalette(nLeft,nRight,nBpp);

		   m_nThreadType = eThdType_RefDisplay;
		   DDThread3::SetRunEvent();
		}
		break;
	default:
		break;
	}

	return nRet;
}


int xhImgAnaly::GetHistogramInfo(zkImgHistogram **ppHistogram)
{
	DD_AUTO_LOCK xLock(&m_Sect);
// 	if( NULL == m_pImgBit )
// 	{
// 		return DD_ERR_FAILURE;
// 	}
// 
// 	return m_pImgBit->GetHistogramInfo(ppHistogram);
	return 0;
}



int xhImgAnaly::CheckPoint(int nPoint,zkImgBitInfo *pImgInfo,BOOL bShareMem,BOOL bShowImg)
{
	if( NULL == pImgInfo )
	{
		return DD_ERR_PARAMETER;
	}

	int nRet = 0;

	DD_AUTO_LOCK xLock(&m_Sect);

	do 
	{
		nRet = xkRefPointMan::SelectPoint(nPoint);
		if( DD_ERR_SUCCESS != nRet )
		{
			break;
		}
		xkImageAnaly::m_pRefPoint = xkRefPointMan::m_pActivePoint;

		nRet = xkImageAnaly::SetChkBuffer(pImgInfo,bShareMem);
		if( DD_ERR_SUCCESS != nRet )
		{
			break;
		}

	}while(false);

	return nRet;
}


inline void xhImgAnaly::Thread_ChkOperate()
{
	DD_AUTO_LOCK xLock(&m_Sect);
	
	if( NULL != m_funMsgCallBack )
	{
		m_funMsgCallBack(AnalyMsg_ParseImage,0,0,0,m_pContext);
	}

	int nRet = xkImageAnaly::ChkOperate();

	if( NULL != m_funMsgCallBack )
	{
		m_funMsgCallBack(AnalyMsg_ParseImage,nRet,0,0,m_pContext);
	}
	
// 	m_nThreadType = eThdType_Nothing;
// 	DDThread3::SetRunEvent(false);
}





inline void xhImgAnaly::Thread_DstOperate()
{
	DD_AUTO_LOCK xLock(&m_Sect);

	if( NULL != m_funMsgCallBack )
	{
		m_funMsgCallBack(AnalyMsg_ParseImage,0,0,0,m_pContext);
	}

	int nRet = xkImageAnaly::DstOperate();

	if( NULL != m_funMsgCallBack )
	{
		m_funMsgCallBack(AnalyMsg_ParseImage,nRet,0,0,m_pContext);
	}

	// 	m_nThreadType = eThdType_Nothing;
	// 	DDThread3::SetRunEvent(false);
}


int xhImgAnaly::SetImgBuffer(int nPoint,zkImgBitInfo *pImgInfo,BOOL bShareMem,BOOL bBlock)
{
	if( NULL == pImgInfo )
	{
		return DD_ERR_PARAMETER;
	}

	int nRet = 0;

	DD_AUTO_LOCK xLock(&m_Sect);

	do 
	{
		nRet = xkRefPointMan::SelectPoint(nPoint);
		if( DD_ERR_SUCCESS != nRet )
		{
			break;
		}
		xkImageAnaly::m_pRefPoint = xkRefPointMan::m_pActivePoint;

		nRet = xkImageAnaly::SetDstBuffer(pImgInfo,bShareMem,bBlock);
		if( DD_ERR_SUCCESS != nRet )
		{
			break;
		}

	}while(false);

	return nRet;
}

int xhImgAnaly::StartParse(int nThreadType)
{
	int nRet = DD_ERR_FAILURE;
	do 
	{
		m_nThreadType = nThreadType;
		DDThread3::SetRunEvent();

	} while (false);

	return 0;
}




